import { customRef } from 'vue'

/**
 * 防抖 ref
 * @param value
 * @param delay
 * @returns
 */
export const useDebouncedRef = <T = string>(value: T, delay = 200) => {
  let timeout: number
  return customRef<T>((track, trigger) => ({
    get() {
      track()
      return value
    },
    set(newValue) {
      clearTimeout(timeout)
      timeout = setTimeout(() => {
        value = newValue
        trigger()
      }, delay)
    },
  }))
}
